package com.ivo.service;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.ivo.dao.HibernateCaller;

public class BaseService<T>
{
	protected Logger log = LoggerFactory.getLogger( this.getClass() );

	@Autowired
	protected HibernateCaller hibernateCaller;

	public void saveInstance(T instance)
	{
		hibernateCaller.getHibernateTemplate().setFlushMode( 2 );
		//----------------------------------------------------------------------------------
		hibernateCaller.saveObject( instance );
	}

	public void updateInstance(T instance)
	{
		hibernateCaller.getHibernateTemplate().setFlushMode( 2 );
		//----------------------------------------------------------------------------------
		hibernateCaller.updateObject( instance );
	}

	public void saveOrUpdateInstance(T instance)
	{
		hibernateCaller.getHibernateTemplate().setFlushMode( 2 );
		//----------------------------------------------------------------------------------
		hibernateCaller.saveOrUpdateObject( instance );
	}

	
	public void removeInstance(T instance)
	{
		hibernateCaller.removeObject( instance );
	}

	public void removeInstance(Class<T> clazz, Serializable id)
	{
		hibernateCaller.removeObject( clazz, id );
	}

	public T getInstance(Class<T> clazz, Serializable id)
	{
		T instance = (T) hibernateCaller.getObject( clazz, id );
		return instance;
	}
	
	public T getInstance(String entityName, Serializable id)
	{
		T instance = (T) hibernateCaller.getObject( entityName, id );
		return instance;
	}

	public List<T> getInstanceSet(String hql)
	{
		List<T> list = (List<T>) hibernateCaller.find( hql );
		//--------------------------------------------------------------------
		return list;
	}

	public Logger getLogger()
	{
		return this.log;
	}

}
